package com.njwt.cloud.mszuul.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.apache.commons.lang.StringUtils;
import com.netflix.zuul.context.RequestContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author <a href="mailto:xingxiao@gtmap.cn">xingxiaofeng</a>
 * @version2.1 2022-04-12
 * @description
 */
public class PermissionFilter extends ZuulFilter {
    private  static  final String FILTERTYPE="pre";
    @Override
    public String filterType() {
        return FILTERTYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() throws ZuulException {
        // TODO Auto-generated method stub
        //获得HttpServletRequest对象
        RequestContext cc = RequestContext.getCurrentContext();
        HttpServletRequest request = cc.getRequest();
        //获得HttpServletResponse对象
        HttpServletResponse response = cc.getResponse();
        //获得客户端提交的Token进行验证
        String token=request.getParameter("token");
        if(StringUtils.isEmpty(token) || !token.equals("123456")) {
            //不对该请求进行路由，不会将请求转发到后端
            cc.setSendZuulResponse(false);
            //设置错误状态码
            cc.setResponseStatusCode(401);
            //设置响应体,也可以将响应的数据封装为json串进行响应
            cc.setResponseBody("权限不足.....");

            response.setContentType("text/html;charset=UTF-8");

            //设置响应头信息
            //cc.addZuulRequestHeader("Content-Type",
            //MediaType.TEXT_HTML_VALUE+";charset=UTF-8");
            //ZuulFilterResult
        }

        return null;
    }
}
